/* ============================================================================= */
/*        FIGURE F1 - TREND RELATIVE COEFFICIENT PLOTS                            */
/*        Matching: PSM Nearest Neighbor - Unsecured Outside Bank Loan          */
/* ============================================================================= */

clear all

/* Load dataset containing firms matched using nearest neighbor PSM */
use "$output\matched_sample_nn_unsecured_outside_loan_1.dta"

/* Restrict the dataset to the period from January 2014 to December 2019 */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep only firms within -12 to +24 months of the event (loan decision) */
keep if inrange(deltamonths, -12, 24)

/* Create financial variables: total loan, leasing, and their log transformations */
gen loan = stloan + ltloan + bm + bi + oc  // Total loan amount
gen leasing = bm + bi                      // Leasing component of the loan

/* Log transformations of financial variables */
gen lloan = log(loan)                      // Log of total loan
gen lstloan = log(stloan)                   // Log of short-term loan
gen lltloan = log(ltloan)                   // Log of long-term loan
gen lavailable = log(oc)                    // Log of available credit
gen ldrawn = log(stloan + ltloan + leasing) // Log of drawn credit
gen lleasing = log(leasing)                 // Log of leasing
gen lco = log(co)                       // Log of used credit lines
gen lother = log(loan - co - ltloan)        // Log of other loan components
gen clusage = co / (co + oc)                // Collateral usage ratio

/* Start logging */
capture log close
log using "$logs\trend_visual_psm_unsecure_outside_bank_loan.log", replace

/* ============================================================================= */
/*        TREND RELATIVE COEFFICIENT PLOT - FIGURE 5                            */
/* ============================================================================= */

preserve

/* Transform the `deltamonths` variable into a categorical variable with non-negative values */
replace delta = deltamonths + 12

/* Define labels for the delta variable */
capture label drop lab_delta
sort siren delta
capture drop nb
bys siren delta : gen nb = _N
su nb, d
drop nb

/* Create value labels to display delta months correctly on the graph */
forvalues n = 0(1)36 {
    local displayed_value = `n' - 12
    label define lab_delta `n'  "`displayed_value'", add
}

label val delta lab_delta

/* ============================================================================ */
/*        PLOT: TOTAL LOAN - FIGURE IA F1                                       */
/* ============================================================================ */

foreach var in lloan {
    /* Run a regression for each variable */
    qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

    /* Generate coefficient plots */
    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" ///
           28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) xsize(6) ysize(4) ylabel(,nogrid) ///
    ylabel(-0.2(0.1)0.3) plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")
    
    /* Save and export the graph */
    graph save "Graph" "$figures/Figure_F1_Trend_relative_`var'_coefplot_psm_nn_unsecure_outside_bank_loan.gph", replace
    graph export "$figures/Figure_F1_Trend_relative_`var'_coefplot_psm_nn_unsecure_outside_bank_loan.pdf", replace
}

/* ============================================================================ */
/*  PLOT: COLLATERAL, LONG-TERM LOAN, OTHER LOAN COMPONENTS, CREDIT LINEUSAGE   */
/* ============================================================================ */

label val delta lab_delta
foreach var in lco lltloan lother clusage {
    /* Run a regression for each variable */
    qui eststo `var' : reghdfe `var' i1.p2p##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

    /* Generate coefficient plots */
    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.p2p#*) rename(1.p2p#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" ///
           28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) xsize(6) ysize(4) ylabel(,nogrid) ///
    ylabel(-0.4(0.2)0.6) plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")
    
    /* Save and export the graph */
    graph save "Graph" "$figures/Figure_F1_Trend_relative_`var'_coefplot_psm_nn_unsecure_outside_bank_loan.gph", replace
    graph export "$figures/Figure_F1_Trend_relative_`var'_coefplot_psm_nn_unsecure_outside_bank_loan.pdf", replace
}

restore

/* Close log file */
log close
